<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$return['ischecklogin']=$this->model('member')->checklogin('','','','',true);
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	set_html('title','升级会员');
	$member1=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('credit1','credit2','mobile','groupid','avatar','nickname'));
	$member=pdo_get('jy_weishop_member',array('mid'=>$_SESSION['uid'],'merchid'=>0,'uniacid'=>$_SESSION['uniacid']));
	$member=array_merge($member1,$member);
	$levelid=$member['level'];
	$mylevel=pdo_get('jy_weishop_member_level',array('id'=>$levelid,'uniacid'=>$_SESSION['uniacid']));
	$member['cardbg']=tomedia($mylevel['cardbg']);
	$member['leveltitle']=$mylevel['title'];
	if($_RQ['xufee']){
		$nextlevel=$mylevel;
	}else{
		$nextlevel=$this->model('member')->get_nextlevel(0,true);
	}
	$nextlevel['goods_ids']=json_decode($return['nextlevel']['goods_ids'],true);
	foreach($nextlevel['goods_ids'] as &$gv){
		$gv['price']=pdo_getcolumn('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$gv['id']),'price');
	}
	$parms=array('merchid'=>0,'uniacid'=>$_SESSION['uniacid'],'status'=>1);
	if($parms){
		if($member['viptime'] && $member['viptime']-TIMESTAMP>30*24*3600){
			$parms['level >']=$mylevel['level'];
		}
	}
	$levels=pdo_getall('jy_weishop_member_level',$parms,'','','level asc');
	$nowid=array();
	foreach($levels as &$av){
		$av=$this->model('member')->get_levelinfo($av['id']);
		foreach($av['goods_ids'] as &$gv){
			$gv['price']=pdo_getcolumn('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$gv['id']),'price');
		}
		$nowid[$av['id']]=pdo_getcolumn('jy_weishop_member_viplog',array('level_id'=>$av['id'],'mid'=>$_SESSION['uid'],'ispay'=>0,'status'=>0),'id');
		$av['cardbg']=tomedia($av['cardbg']);
		$av['vipimgs']=tomedia($av['vipimgs']);	
	}
	$post=array('level_id'=>$nextlevel['id']);
	$post['paytype']=1;
	$post['num']=1;
	$post['checked']=false;
	$return['xieyi']=$this->plugin_setting('systemxieyi');
	$return['post']=$post;
	$return['member']=$member;
	$return['levels']=$levels;
	$nowid[$nextlevel['id']]=pdo_getcolumn('jy_weishop_member_viplog',array('level_id'=>$nextlevel['id'],'mid'=>$_SESSION['uid'],'ispay'=>0,'status'=>0),'id');
	$return['nowid']=$nowid;
	$nextlevel['cardbg']=tomedia($nextlevel['cardbg']);
	$nextlevel['vipimgs']=tomedia($nextlevel['vipimgs']);	
	$return['nowlevel']=$nextlevel;
	$return['post']=$post;
	$data=array();
	$data['isordermoney']=pdo_getsum('jy_weishop_order',array('mid'=>$_SESSION['uid'],'ispay'=>1),'price');
	$data['isordercount']=pdo_count('jy_weishop_order',array('mid'=>$_SESSION['uid'],'ispay'=>1));
	$return['data']=$data;
	$nowplugin=$_SESSION['current_plugin']?:'jy_weishop';
	$menus_index=$_SESSION['menus_index']?:$nowplugin.'/index/';
	$return['indexaction']=$menus_index;
	$return['homeaction']='jy_weishop/member.index/';
	if($_RQ['fromplugin']){
		$return['indexaction']=$_RQ['fromplugin'].'/index/';
		$return['homeaction']=$_RQ['fromplugin'].'/member.index/';
	}
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$level_id=$postdata['level_id'];
	$levelinfo=pdo_get('jy_weishop_member_level',array('id'=>$level_id,'uniacid'=>$_SESSION['uniacid']));
	if($levelinfo['up_type']<2){
		exi('该等级不能付费开通！','error');
	}
	$fee=$levelinfo['open_fee'];
	if($fee<=0){
		exi('该等级不能付费开通！','error');
	}
	//创建一个会员订单
	$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'level_id'=>$levelinfo['id'],
			'ordersn'=>get_ordersn('jy_weishop_member_viplog','ordersn'),
			'fee'=>$fee,
			'ispay'=>0,
			'status'=>0,
			'createtime'=>TIMESTAMP,
	);
	pdo_insert('jy_weishop_member_viplog',$insert);
	$order_id=pdo_insertid();
	$params=array(
			'id'=>$order_id,
			'title'=>'开通商城会员',
			'fee'=>$fee,
			//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
			'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
			'back_url'=>'jy_weishop/member.open/payresult/',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
			'success_url'=>'jy_weishop/member.index/',
			'error_url'=>'jy_weishop/member.index/',
	);
	$payid=cfc('pay')->create_pay_order($params);
	pdo_update('jy_weishop_member_viplog',array('pay_id'=>$payid),array('id'=>$order_id));
	$res=array(
			'pay_id'=>$payid,
			'order_id'=>$order_id,
			'success_url'=>$success_url,
			'error_url'=>$error_url,
	);
	exi($res);
}
if($_RQ['op']=='pay'){
	$id = intval($_RQ['id']);
	if(!$id){
		exi('订单不存在，请重新选择订单','error');
	}	
	$order=pdo_get('jy_weishop_member_viplog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$nextlevel=$this->pdo_get('member_level',array('id'=>$order['level_id'],'uniacid'=>$_SESSION['uniacid']));
	if($nextlevel['up_type']<2){
		exi('该等级不能付费开通！','error');
	}
	if($nextlevel['open_fee']<=0){
		exi('该等级不能付费开通！','error');
	}
	if($nextlevel['open_fee']!=$order['fee']){
		pdo_update('jy_weishop_member_viplog',array('fee'=>$nextlevel['open_fee']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		pdo_update('core_paylog',array('fee'=>$nextlevel['open_fee']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['pay_id']));
	}
	exi($order['pay_id']);
}
if($_RQ['op']=='payresult'){
	$payresult=$_RQ['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error');
	}
	$order=pdo_get('jy_weishop_member_viplog',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！购买会员订单不存在！','error');
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！购买会员订单金额不匹配！','error');
	}
	pdo_update('jy_weishop_member_viplog',array('status'=>1,'ispay'=>1,'paytime'=>TIMESTAMP,'paytype'=>$payresult['pay_type']),array('id'=>$order['id']));
	//计算到期时间
	$startTime =time();
	$member=pdo_get('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$order['mid']));
	if($member['level']==$order['level_id'] && $member['viptime']>TIMESTAMP){
		$addTime=$member['viptime']+365*24*3600;
	}else{
		$addTime =TIMESTAMP+365*24*3600;
	}
	pdo_update('jy_weishop_member',array('isvip'=>1,'viptime'=>$addTime,'level'=>$order['level_id']),array('mid'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']));
	$levelinfo=pdo_get('jy_weishop_member_level',array('id'=>$order['level_id'],'uniacid'=>$_SESSION['uniacid']));
	$this->model('member')->up_member_level($order['mid'],$order['level_id'],$member['merchid'],$levelinfo['up_type']);
	$this->model('member')->up_member_openfee($member['mid'],$order['level_id']);
	$this->model('store')->up_workerka($order);
	if($this->plugin_exists('jy_area_psi')){
		model('jy_area_psi/commission')->create_fee($order,4);
	}
	exi('支付成功','success','jy_weishop/member.index/');
}